package com.hex.ds.em.hcdp.gauss.analyze.script.performance;

import static com.hex.ds.em.hcdp.gauss.pack.service.impl.ScriptPackTaskServiceImpl.wordResult;

import cn.hutool.core.util.StrUtil;
import com.hex.ds.em.hcdp.basic.common.IResultForReport;
import com.hex.ds.em.hcdp.gauss.analyze.script.performance.entity.CheckExistsAndInEntity;
import com.hex.ds.em.hcdp.gauss.common.entity.ScriptPackScanResultEntity;
import com.hex.ds.em.hcdp.gauss.common.enums.AntlrEnum;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParser.Compound_expressionContext;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParser.Quantified_expressionContext;
import com.hex.hlrt.gauss.g4.basic.gen.GaussParserBaseListener;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 * @Package com.hex.ds.em.hcdp.gauss.analyze.script.performance
 * @ClassName CheckUnionAllListener
 * @Description Exists和In检查
 * @Author gj.xu
 * @Date 2024/9/14 15:52
 * @Version v2.0
 **/
@Slf4j
@Accessors(chain = true)
@Scope(value = "prototype")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service("Gauss-CheckExistsAndIn")
public class CheckExistsAndInListener extends GaussParserBaseListener implements IResultForReport {

  @Override
  public void exitCompound_expression(Compound_expressionContext ctx) {
    if (ctx.IN() != null && ctx.in_elements() != null && ctx.in_elements().subquery() != null &&
        !StrUtil
            .containsAnyIgnoreCase(ctx.in_elements().subquery().getText(), "row_number")
        && !StrUtil.containsAnyIgnoreCase(ctx.in_elements().subquery().getText(), "group by")
        && !StrUtil.containsAnyIgnoreCase(ctx.in_elements().subquery().getText(), "distinct")
    ) {
      // 更新扫描后word指标结果
      setResultForWordReport();

      CheckExistsAndInEntity ruleEntity = new CheckExistsAndInEntity();
      ctx.addHeadAnyChild(ruleEntity);
      log.info("rule-[CheckExistsAndIn]:");
    }
  }


  @Override
  public void exitQuantified_expression(Quantified_expressionContext ctx) {
    if (ctx.EXISTS() != null && ctx.select_only_statement() != null && !StrUtil
        .containsAnyIgnoreCase(ctx.select_only_statement().getText(), "row_number")
        && !StrUtil.containsAnyIgnoreCase(ctx.select_only_statement().getText(), "group by")
        && !StrUtil.containsAnyIgnoreCase(ctx.select_only_statement().getText(), "distinct")) {
      // 更新扫描后word指标结果
      setResultForWordReport();

      CheckExistsAndInEntity ruleEntity = new CheckExistsAndInEntity();
      ctx.addHeadAnyChild(ruleEntity);
      log.info("rule-[CheckExistsAndIn]:");
    }
  }


  @Override
  public void setResultForWordReport() {
    ScriptPackScanResultEntity result = wordResult
        .get(AntlrEnum.EXISTS_AND_IN_CHECK.getValue());
    if (result == null) {
      return;
    }
    result.setScanTable("").setScanState("fail");
    wordResult.put(AntlrEnum.EXISTS_AND_IN_CHECK.getValue(), result);
  }
}
